﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

public class StreamClass
{
    public StringBuilder builder = new StringBuilder();

    List<string> filter = new List<string>()
    {
      "Log"
    };

    public bool Intersect(string name)
    {
        foreach (var item in filter)
        {
            if (name.Contains(item))
                return true;
        }
        return false;
    }

    public StreamClass(string folder, int order)
    {

        if (!Directory.Exists(Application.dataPath + "/StreamingAssets"))
        {
            Debug.LogError($"不存在文件夹 StreamingAssets");
            return;
        }

        DirectoryInfo directory = new DirectoryInfo(folder);

        if (directory == null)
        {
            Debug.LogError($"不存在文件夹{folder}");
            return;
        }

        var files      = directory.GetFiles();
        var directorys = directory.GetDirectories();
        var className  = directory.Name == CreatUtil.stream_FD ? "StreamFolder" : CreatUtil.GetClassString(directory.Name);

        builder.Clear();
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}//自动生成类，不能修改");
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}public class {className}");
        builder.AppendLine($"  {CreatUtil.GetFormat(order)}{CreatUtil.LeftSymbol}");

        List<string> fields = new List<string>();

        foreach (var item in files)
        {
            if (Intersect(item.Name)||item.FullName.Contains(".meta"))
                continue;

   

            var outName = Path.GetFileNameWithoutExtension(item.Name);
            var outFullName = CreatUtil.SubstringSingle(item.FullName, CreatUtil.stream_FD);
            var outFullNameSuf = $"{CreatUtil.SubstringSingle(Path.GetDirectoryName(item.FullName), CreatUtil.stream_FD)}/{outName}".TrimStart('/');

            fields.Add(outName);
            fields.Add($"{outName}_Suf");

            Debug.Log(CreatUtil.SetStringFieldSuf(outName, outFullName, order));

            builder.AppendLine(CreatUtil.SetStringField(outName, outFullNameSuf, order));
            builder.AppendLine(CreatUtil.SetStringFieldSuf(outName, outFullName, order));
        }

        foreach (var item in directorys)
        {
            if (Intersect(item.Name) || item.FullName.Contains(".meta"))
                continue;

            var dirClass = CreatUtil.CreatStreamClass(CreatUtil.SubstringToAssetsPath(item.FullName), order + 2);
            builder.AppendLine($"{dirClass.builder}");
        }

        builder.AppendLine($"  {CreatUtil.GetFormat(order)}{CreatUtil.RightSymbol}");
    }
}
